package com.come4loves.game.module.card.model;

import java.util.ArrayList;
import java.util.List;

import com.come4loves.core.utils.Splitable;
import com.come4loves.core.utils.StringUtils;
import com.come4loves.game.dataconfig.service.GeneralCardService;
import com.come4loves.game.dataconfig.service.SystemService;
import com.come4loves.game.module.card.constant.CardType;

public class CardGroupModel {

	/**
	 * 卡片组id
	 */
	public int groupId;
	
	/**
	 * 卡牌组名称
	 */
	public String name;
	/**
	 * 卡牌组
	 */
	public List<CardModel> cardList = new ArrayList<CardModel>();
	
	/**
	 * 卡牌组标记(扩展)
	 */
	public byte groupFlag;
	
	
	public static CardGroupModel valueOf(String groupString) {
		CardGroupModel cardGroupModel = new CardGroupModel();
		if (StringUtils.isNotBlank(groupString)) {
			String[] groups = StringUtils.split(groupString, Splitable.BETWEEN_UNDERLINE);
			String[] values = StringUtils.fillStringArray(groups, 4, "0");
			cardGroupModel.groupId = Integer.valueOf(values[0]);
			cardGroupModel.name = values[1] == null? "" : values[1];
			cardGroupModel.cardList = StringUtils.delimiterString2List(values[2], CardModel.class, Splitable.DELIMITER_ARGS);
			cardGroupModel.groupFlag = Byte.valueOf(values[3]);
		}
		return cardGroupModel;
	}
	
	public static CardGroupModel valueOf(int id, String name, List<CardModel> cardModels, byte flag, String desc) {
		CardGroupModel cardGroupModel = new CardGroupModel();
		cardGroupModel.groupId = id;
		cardGroupModel.name = name == null? "" : name;
		cardGroupModel.cardList = cardModels;
		cardGroupModel.groupFlag = flag;
		return cardGroupModel;
	}
	
	
	
	@Override
	public String toString() {
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append(groupId).append(Splitable.BETWEEN_UNDERLINE);
		sBuffer.append(name).append(Splitable.BETWEEN_UNDERLINE);
		String cards = StringUtils.delimiterTCollection2String(this.cardList, Splitable.DELIMITER_ARGS);
		sBuffer.append(cards).append(Splitable.BETWEEN_UNDERLINE);
		sBuffer.append(groupFlag);
		return sBuffer.toString();
	}
	
	public CardModel getCardModel(CardType cardType, int cardId) {
		if (cardList.isEmpty()) {
			return null;
		}
		for (CardModel cardModel : cardList) {
			if (cardModel.type == cardType && cardId == cardModel.cfgId) {
				return cardModel;
			}
		}
		return null;
	}
	
	public boolean addCardModel(CardModel cardModel) {
		if (this.isFull()) {
			return false;
		} else {
			return this.cardList.add(cardModel);
		}
	}
	
	public int getCardCount(CardType cardType, int cardId) {
		if (cardList.isEmpty()) {
			return 0;
		}
		int count = 0;
		for (CardModel cardModel : cardList) {
			if (cardModel.type == cardType && cardId == cardModel.cfgId) {
				count += cardModel.count;
			}
		}
		return count;
	}
	
	public int getCardsNum() {
		int size = this.cardList.size();
		CardModel mainGeneral = this.getMainGeneral();
		if (mainGeneral != null) {
			if (GeneralCardService.get(mainGeneral.cfgId).getType() >= 1) {
				return size - 2;
			} else {
				return size - 1;
			}
		} else {
			return size;
		}
	}
	
	public boolean isFull() {
		return this.getCardsNum() >= SystemService.systemConfig.getCardGroupMaxCardNum();
	}
	
	public CardModel getMainGeneral() {
		for (CardModel cardModel : cardList) {
			if (cardModel.type == CardType.GENERAL) {
				return cardModel;
			}
		}
		return null;
	}
}
